3 Building Web Maps in R Studio

To create a webmap/interactive map, simply use the tmap_mode function to set the mode to view:

tmap_mode("view")
## tmap mode set to interactive viewing

Now, when you plot the map, it will appear interactive:

usa_trifecta_map
## Credits not supported in view mode.
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.

To shift back to a static map, set the mode back to plot:

tmap_mode("plot")
## tmap mode set to plotting

And now, when printing the map again, it will be displayed again as a static map.

usa_trifecta_map

The webmap looks a little strange with Alaska and Hawaii shifted. So, let’s make a webmap that shifts Alaska and Hawaii back to their actual locations.

To do so, we’ll read in the shapefile with Alaska and Hawaii in their proper locations, and assign it to an object named usa_unshifted.

usa_unshifted<-st_read("usa_unshifted.shp")
## Reading layer `usa_unshifted' from data source `/Users/adra7980/Documents/git_repositories/gistools_qda/data/usa_unshifted.shp' using driver `ESRI Shapefile'
## Simple feature collection with 52 features and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
## geographic CRS: NAD83
usa_unshifted
## Simple feature collection with 52 features and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
## geographic CRS: NAD83
## First 10 features:
##    GEOID           NAME abbrev                       geometry
## 1     23          Maine     ME MULTIPOLYGON (((-67.61976 4...
## 2     25  Massachusetts     MA MULTIPOLYGON (((-70.83204 4...
## 3     26       Michigan     MI MULTIPOLYGON (((-88.68443 4...
## 4     30        Montana     MT MULTIPOLYGON (((-104.0577 4...
## 5     32         Nevada     NV MULTIPOLYGON (((-114.0506 3...
## 6     34     New Jersey     NJ MULTIPOLYGON (((-75.52684 3...
## 7     36       New York     NY MULTIPOLYGON (((-71.94356 4...
## 8     37 North Carolina     NC MULTIPOLYGON (((-82.60288 3...
## 9     39           Ohio     OH MULTIPOLYGON (((-82.81349 4...
## 10    42   Pennsylvania     PA MULTIPOLYGON (((-75.41504 3...

Then, we’ll go through the same steps as above, to essentially recreate our map with this new spatial dataset. When we go back to View mode and print our new map object, we’ll see a dynamic map with Hawaii and Alaska in their original locations.

# join tabular data to "usa_unshifted"
usa_unshifted_trifecta<-inner_join(usa_unshifted, usa_trifecta_data, by="abbrev")

# sets factor
usa_unshifted_trifecta<-usa_unshifted_trifecta %>% 
                          mutate(Composition=factor(Composition, levels=c("Democratic Trifecta", 
                                                  "Republican Trifecta", "Divided Government"))) %>% 
                          rename(State=NAME.x) %>% 
                          relocate(State, Composition)



# recreate map
usa_trifecta_map_unshifed<-tm_shape(usa_unshifted_trifecta)+
                                tm_polygons(col="Composition", 
                                            palette=colors,
                                            title="")+
                                tm_layout(frame=FALSE,
                                          main.title="Partisan Composition of State Governments, 2022",
                                          main.title.size=1,
                                          main.title.position="center")

# change to view mode
tmap_mode("view")
## tmap mode set to interactive viewing
usa_trifecta_map_unshifed
## tmap mode set to plotting